package org.test;

import java.util.Iterator;
import java.util.StringTokenizer;

import edu.utexas.mapreduce.Mapper;
import edu.utexas.mapreduce.OutputWriter;
import edu.utexas.mapreduce.Reducer;

public class WordCount implements Mapper, Reducer {

	public WordCount() {
	}
	
	public void map(String key, String value, OutputWriter result) {
		StringTokenizer tokenizer = new StringTokenizer(value);
	    while (tokenizer.hasMoreTokens()) {
	       result.write(tokenizer.nextToken(), "1");
	    }
	}

	public void reduce(String key, Iterator<String> values, OutputWriter result) {
		int sum = 0;
		while (values.hasNext()) {
			sum += Integer.parseInt(values.next());
		}
		result.write(key, Integer.toString(sum));
	}

}
